const express = require("express");
const {cors,json,urlencoded} = require("./middleware");
const app = express();
app.use(cors());
// // application/json
// app.use(function(req,res,next){
// 	if(req.method==="POST"){
// 		req.body = "";
// 		req.on("data",function(thunk){
// 			req.body+=thunk;
// 		});
// 		req.on("end",function(){
// 			if(req.headers["content-type"]==="application/json"){
// 				req.body = JSON.parse(req.body);
// 			}
// 			next();
// 		})
// 	}else{
// 		next();
// 	}
// })
// // application/x-www-form-urlencoded
// app.use(function(req,res,next){
// 	if(req.method==="POST"){
// 		req.body = "";
// 		req.on("data",function(thunk){
// 			req.body+=thunk;
// 		});
// 		req.on("end",function(){
// 			if(req.headers["content-type"]==="application/x-www-form-urlencoded"){
// 				// a=1&b=2&c=3 ===>[a=1,b=2,c=3]
// 				const obj = {};
// 				req.body.split("&").forEach(item=>{
// 					const [key,value] = item.split("=")
// 					obj[key] = value;
// 				})
// 				req.body = obj;
// 			}
// 			next();
// 		})
// 	}else{
// 		next();
// 	}
// })





app.use(json())

app.use(urlencoded())

// app.get("/my",(req,res)=>{
// 	const {a,b} = req.query;
// })




app.post("/info",(req,res)=>{
	console.log(req.body);
	res.json({
		ok:1,
		msg:"success"
	})

})
app.get("/info",(req,res)=>{
	res.json({
		ok:100,
		msg:"info->success"
	})
});
app.get("/sum",(req,res)=>{
	res.json({
		ok:200,
		msg:"sum->success"
	})
})
app.listen(80,function(){
	console.log("success");
})